package main;

import javax.swing.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import javax.swing.tree.TreePath;

public class DirectoryModel extends javax.swing.table.AbstractTableModel {

    protected File directory;
    protected ArrayList<String> children;
    protected ArrayList<String> tooltips;
    protected int rowCount;
    protected Object dirIcon;
    protected Object fileIcon;

    public DirectoryModel() {
        init();
    }

    public DirectoryModel(File dir) {
        init();
        directory = dir; 
        children = new ArrayList<String>();
        Collections.addAll(children, dir.list());        
        rowCount = children.size();
        tooltips = new ArrayList<String>();
    }

    protected void init() {
        dirIcon = UIManager.get("FileView.directoryIcon");
        fileIcon = UIManager.get("FileView.fileIcon");
    }

    public void setDirectory(File dir) {
        if (dir != null) {
            directory = dir;
            children.clear();
            Collections.addAll(children, dir.list());
            rowCount = children.size();
        } else {
            directory = null;
            children.clear();
            rowCount = 0;
        }
        fireTableDataChanged();
    }

    public int getRowCount() {
        return !children.isEmpty() ? rowCount : 0;
    }

    public int getColumnCount() {
        return !children.isEmpty() ? 4 : 0;
    }
    
    public String getTooltip(int row) {
        return tooltips.get(row);
    }

    public Object getValueAt(int row, int column) {
        if (directory == null || children.isEmpty()) {
            return null;
        }

        File fileSysEntity = new File(directory, children.get(row));
        tooltips.add(row, fileSysEntity.getAbsolutePath());
       
        switch (column) {
            case 0:
                return fileSysEntity.isDirectory() ? dirIcon : fileIcon;
            case 1:
                return fileSysEntity.getName();
            case 2:
                if (fileSysEntity.isDirectory()) {
                    return "--";
                } else {                    
                    return new Long(fileSysEntity.length());
                }
            case 3:
                Date date = new Date(fileSysEntity.lastModified());
                SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy hh:mm");
                String dateString = sdf.format(date);
                return dateString;
            default:
                return "";
        }
    }
    
    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Type";
            case 1:
                return "Name";
            case 2:
                return "Bytes";
            case 3:
                return "Modified";
            default:
                return "unknown";
        }
    }

    @Override
    public Class getColumnClass(int column) {
        if (column == 0) {
            return getValueAt(0, column).getClass();
        } else {
            return super.getColumnClass(column);
        }
    }

    public Object getRoot() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Object getChild(Object parent, int index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getChildCount(Object parent) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean isLeaf(Object node) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void valueForPathChanged(TreePath path, Object newValue) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getIndexOfChild(Object parent, Object child) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}